package com.sluggard.security.core.config;

import com.sluggard.security.core.filter.UserLoadFilter;
import com.sluggard.security.properties.SecurityConfigProperties;
import jakarta.annotation.Resource;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import java.util.Arrays;
import java.util.List;

/**
 * @author lizheng
 * @since 2024.4.22
 */
@Configuration
@EnableWebSecurity
@EnableConfigurationProperties({SecurityConfigProperties.class})
public class SecurityConfig {

    private static final List<String> DEFAULT_PERMIT_URLS = Arrays.asList("/manage/loginout/login", "/manage/loginout/getToken", "/manage/loginout/captcha", "/manage/loginout/needCaptcha", "/doc.html", "/webjars/**", "/v3/api-docs/**", "/swagger-ui/**","/actuator/**") ;

    @Resource
    private AuthenticationConfiguration config;

    @Resource
    private UserDetailsService userDetailsService;

    @Resource
    private PasswordEncoder passwordEncoder;

    @Resource
    private UserLoadFilter userLoadFilter;

    @Resource
    private SecurityConfigProperties securityConfigProperties;

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        securityConfigProperties.getPermitUrls().addAll(DEFAULT_PERMIT_URLS);
        http
                .authorizeHttpRequests((authorize) -> authorize
                        .requestMatchers(securityConfigProperties.getPermitUrls().toArray(new String[]{}))
                        .permitAll()
                        .anyRequest()
                        .authenticated()
                )
                .authenticationProvider(authenticationProvider())
                .formLogin(AbstractHttpConfigurer::disable)
                .csrf(AbstractHttpConfigurer::disable)
                .httpBasic(AbstractHttpConfigurer::disable)
                .sessionManagement(AbstractHttpConfigurer::disable)
                .logout(AbstractHttpConfigurer::disable)
                .addFilterBefore(userLoadFilter, UsernamePasswordAuthenticationFilter.class);
        return http.build();
    }

    @Bean
    public AuthenticationManager authenticationManager() throws Exception {
        return config.getAuthenticationManager();
    }

    @Bean
    public AuthenticationProvider authenticationProvider() {
        DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider();
        authenticationProvider.setUserDetailsService(userDetailsService);
        authenticationProvider.setPasswordEncoder(passwordEncoder);
        return authenticationProvider;
    }

}
